package com.xwm93up.basic.lambdatips.collectors;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.Comparator.comparing;

/**
 * @Description: desc
 * @Author: xwm93up
 * @Since: 2023/2/28 14:32
 */
public class CollectorsTest {

    /**
     * 装换成值
     */
    @Test
    public void test1 () {
        ArrayList<Student> students1 = new ArrayList<>(3);
        students1.add(new Student("路飞", 23 ,175, null));
        students1.add(new Student("红发", 40 ,180, null));
        students1.add(new Student("白胡子", 50 ,185, null));

        OutstandingClass outstandingClass1 = new OutstandingClass("一班", students1);
        // 复制stu1,并且移除一个学生
        ArrayList<Student> students2 = new ArrayList<>(students1);
        students2.remove(1);
        OutstandingClass outstandingClass2 = new OutstandingClass("二班", students2);
        // 将stu1,stu2装换为Stream
        Stream<OutstandingClass> classSteam = Stream.of(outstandingClass1, outstandingClass2);
        OutstandingClass outstandingClass = biggestGroup(classSteam);
        System.out.println("人数最多的班级是:" + outstandingClass.getName());

        System.out.println("一班平均年龄是:" + averageNumberOfStudent(students1));
    }

    /**
     * 获取人数最多的班级
     * @param students1
     * @return
     */
    private double averageNumberOfStudent(ArrayList<Student> students1) {
        return students1.stream().collect(Collectors.averagingInt(Student::getAge));
    }

    /**
     * 计算平均年龄
     *
     * @param outstandingClassStream
     * @return
     */
    private OutstandingClass biggestGroup(Stream<OutstandingClass> outstandingClassStream) {
        return outstandingClassStream.collect(
                Collectors.maxBy(comparing(ostClass -> ostClass.getStudents().size())))
                .orElseGet(OutstandingClass::new);
    }

    /**
     * 装换成块
     */
    @Test
    public void partitioningTest() {
        ArrayList<Student> students = new ArrayList<>(3);
        students.add(new Student("路飞", 23, 175, Arrays.asList(SpecialityEnum.SING)));
        students.add(new Student("红发", 40 ,180, Arrays.asList(SpecialityEnum.DANCE)));
        students.add(new Student("白胡子", 50 ,185, Arrays.asList(SpecialityEnum.CALLIGRAPHY)));

        Map<Boolean, List<Student>> collect = students.stream().collect(
                Collectors.partitioningBy(student -> student.getSpecialities().contains(SpecialityEnum.SING)));
        System.out.println(collect);
    }

    /**
     * 数据分组
     * 根据学生特长进行分组
     */
    @Test
    public void groupingByTest() {
        ArrayList<Student> students = new ArrayList<>(3);
        students.add(new Student("路飞", 23, 175, Arrays.asList(SpecialityEnum.SING)));
        students.add(new Student("红发", 40 ,180, Arrays.asList(SpecialityEnum.DANCE)));
        students.add(new Student("白胡子", 50 ,185, Arrays.asList(SpecialityEnum.DANCE)));

        Map<Enum, List<Student>> collect = students.stream().collect(Collectors.groupingBy(student -> student.getSpecialities().get(0)));
        System.out.println(collect);
    }

    /**
     * 字符串拼接
     */
    @Test
    public void joiningTest() {
        ArrayList<Student> students = new ArrayList<>(3);
        students.add(new Student("路飞", 23, 175, Arrays.asList(SpecialityEnum.SING)));
        students.add(new Student("红发", 40, 180, Arrays.asList(SpecialityEnum.DANCE)));
        students.add(new Student("白胡子", 50 ,185, Arrays.asList(SpecialityEnum.DANCE)));

        String collect = students.stream().map(Student::getName).collect(Collectors.joining(",", "[", "]"));
        System.out.println(collect);
    }
}
